package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
//    分页查询
    @PreAuthorize("hasAuthority('ORDERLIST_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult=orderService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }
//    后台新增预约
@PreAuthorize("hasAuthority('OERDERLIST_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Map map,Integer[] setmealIds){
    System.out.println("setmealIds = " + Arrays.toString(setmealIds));
        if (setmealIds==null || setmealIds.length<=0){
            return new Result(false,MessageConstant.ORDER_NEED_SETMEAL);
        }
        try {
            map.put("setmealIds",setmealIds);
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            return orderService.backendOrder(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
//    确定预约：修改预约状态
@PreAuthorize("hasAuthority('ORDER_UPDATESTATUS')")
    @RequestMapping("/handleOrderStatus")
    public Result handleOrderStatus(Integer id){
        return orderService.editOrderStatus(id);
    }
//    取消预约
@PreAuthorize("hasAuthority('ORDER_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
        }


    }
}